package com.taxshare.tools.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import java.nio.charset.StandardCharsets;

/**
 * Base64
 *
 * @author admin
 * @date 2019/04/29
 */
@Slf4j
public class Base64Util {

    private Base64Util() {
    }

    public static String encode(String input) {
        try {
            input = new String(Base64.encodeBase64(input.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error("base64加密异常", e);
            input = "";
        }
        return input;
    }

    public static String encode(byte[] input) {
        String output = "";
        try {
            output = new String(Base64.encodeBase64(input), StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error("base64加密异常", e);
            output = "";
        }
        return output;
    }

    public static String decode(String input) {
        String output = "";
        try {
            byte[] decodeBase64 = Base64.decodeBase64(input.getBytes(StandardCharsets.UTF_8));
            output = new String(decodeBase64, StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error("base64解密异常", e);
        }
        return output;
    }

}
